<?php

abstract class Skjb_Controller_Action extends Zend_Controller_Action
{
    public static $errors = array();
    
    protected $_flashMessenger = null;
    
    public function init()
    {
        parent::init();
        
        $this->initView();
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        
        $this->view->messages = $this->_flashMessenger->getMessages();
        $this->view->errors = array();
        $this->view->valid = true;
    }
    
    public function addError($name, $message)
    {
        $this->view->errors[$name] = $message;
        self::$errors[$name] = $message;
        $this->view->valid = false;
    }
    
    public function addErrors($messages)
    {
        foreach ($messages as $field => $errors) {
            foreach ($errors as $error) {
                $this->addError($field, $error);
            }
        }
    }
    
    public function isValid()
    {
        return $this->view->valid;
    }
    
    public function __get($name)
    {
        if (substr($name, -5) == 'Table') {
            return Skjb_Db_Table_Registry::getTable($name);
        }
        
        return parent::__get($name);
    }
}